#include "MainMenuState.h"
#include "SGD Wrappers/CSGD_XAudio2.h"

#include "Game.h"
#include "GamePlayState.h"
#include "CreditsState.h"
#include "OptionsState.h"
#include "TutorialState.h"
#include "LoadGameState.h"
#include "tinyxml.h"

MainMenuState::MainMenuState(void)
{
	// Set data members to 'clean' default values
	D3D = nullptr;
	DI  = nullptr;
	TM  = nullptr;
	XA  = nullptr;

	GroupFont = nullptr;

	// -1 == INVALID ID
	FontImageID = -1;
	mainMenuScreenID = -1;
	altMainMenuID = -1;
	menuStuffID = -1;
	menuBoxID = -1;
	easterEggID = -1;

	CursorX = -1;
	CursorY = -1;

	eggFound = false;
}

MainMenuState::~MainMenuState(void)
{

}

void MainMenuState::Enter(void)
{
	// Acess all the SGD Wrappers
	D3D	=	CSGD_Direct3D::GetInstance();
	DI	=	CSGD_DirectInput::GetInstance();
	TM	=	CSGD_TextureManager::GetInstance();
	XA	=	CSGD_XAudio2::GetInstance();

	if(XA->MusicIsSongPlaying(Game::GetInstance()->GetMenuMusic()) == false)
		XA->MusicPlaySong(Game::GetInstance()->GetMenuMusic(),true);

	// Getting group font
	GroupFont =	CBitmapFont::GetInstance();

	// Load resources
	FontImageID = TM->LoadTexture(_T("Graphics/Robovox.bmp"), D3DCOLOR_XRGB(0,0,0));
	//FontImageID = TM->LoadTexture(_T("Graphics/Menu Images/options_menu_stuff.png"), D3DCOLOR_XRGB(0,0,0));
	mainMenuScreenID = TM->LoadTexture(_T("Graphics/Menu Images/Newmain_menu.png"));
	altMainMenuID = TM->LoadTexture(_T("Graphics/Menu Images/alt_main_menu.png"));
	menuStuffID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Menu Images/options_menu_stuffUPDATE.png"));
	menuBoxID = TM->LoadTexture(_T("Graphics/Menu Images/main_menuBox.png"));
	easterEggID = TM->LoadTexture(_T("Graphics/Menu Images/Eggmain_menu.png"));

	// Set the bitmap font's values
	GroupFont->SetValues(32, 32, 16, 14, ' ', FontImageID);

	CursorX = 262;
	CursorY = PLAY;

	eggFound = false;
}

void MainMenuState::Exit(void)
{
	// Unload resource
	if(FontImageID != -1)
	{
		TM->UnloadTexture(FontImageID);
		FontImageID = -1;
	}
	if(mainMenuScreenID != -1)
	{
		TM->UnloadTexture(mainMenuScreenID);
		mainMenuScreenID = -1;
	}
	if(altMainMenuID != -1)
	{
		TM->UnloadTexture(altMainMenuID);
		altMainMenuID = -1;
	}
	if(menuStuffID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(menuStuffID);
		menuStuffID = -1;
	}
	if(menuBoxID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(menuBoxID);
		menuBoxID = -1;
	}
	if(easterEggID != -1)
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(easterEggID);
		easterEggID = -1;
	}



	// Set data members to 'clean' default values
	D3D = nullptr;
	DI  = nullptr;
	TM  = nullptr;
	XA  = nullptr;

	GroupFont = nullptr;

	CursorX = -1;
	CursorY = -1;
}

bool MainMenuState::Input(void)
{
	Game* game = Game::GetInstance();
	// Key Down
	if(DI->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(CursorY == PLAY)
		{
			CursorY = OPTIONS;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
		else if(CursorY != QUIT)
		{
			CursorY += INCREMENT;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
		else
		{
			CursorY = PLAY;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
	}

	// Key Up
	if(DI->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(CursorY == OPTIONS)
		{
			CursorY = PLAY;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
		else if(CursorY != PLAY)
		{
			CursorY -= INCREMENT;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
		else
		{
			CursorY = QUIT;
			CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
		}
	}
	// REDACTED //
	if(DI->JoystickButtonDown(4) && DI->JoystickButtonDown(5) && DI->JoystickButtonDown(0))
	{
		eggFound = true;
		return true;
	}
	/////////////

	// Go to quit when the user presses Escape
	if(DI->KeyPressed(DIK_ESCAPE) || DI->JoystickButtonPressed(6))
	{
		CursorY = QUIT;
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuMove());
	}

	// Enter the gameplay
	if(DI->JoystickButtonPressed(0) && CursorY == PLAY)
	{
		Game::GetInstance()->ChangeState(LoadGameState::GetInstance());
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
	}
	else if(DI->JoystickButtonPressed(0) && CursorY == OPTIONS)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
		OptionsState::GetInstance()->SetFromMain(true);
		Game::GetInstance()->ChangeState(OptionsState::GetInstance());
	}
	else if(DI->JoystickButtonPressed(0) && CursorY == H2P)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
		GamePlayState::GetInstance()->SetFromMain(true);
		GamePlayState::GetInstance()->SetNewGame(true);
		Game::GetInstance()->ChangeState(GamePlayState::GetInstance());
	}
	else if(DI->JoystickButtonPressed(0) && CursorY == CREDITS)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
		Game::GetInstance()->ChangeState(CreditsState::GetInstance());
	}
	else if(DI->JoystickButtonPressed(0) && CursorY == QUIT)
	{
		CSGD_XAudio2::GetInstance()->SFXPlaySound(game->GetMenuSelect());
		return false;
	}

	return true;
}

void MainMenuState::Update(float elapsed)
{

}

void MainMenuState::Render(void)
{
	// Selected and not selected colors
	DWORD isSelected = D3DCOLOR_XRGB(255,0,0);
	DWORD notSelected =  D3DCOLOR_XRGB(0,255,255);

	if(Game::GetInstance()->GetLanguage() == true)
	{
		if(eggFound == false)
			TM->Draw(mainMenuScreenID,0,0);
		else
			TM->Draw(easterEggID,0,0);

		RECT box = {0,0,285,50};
		TM->Draw(menuBoxID,CursorX,CursorY,1.0f,1.0f,&box);
	}
	else
	{
		//{PLAY = 290, OPTIONS = 360, H2P = 402, CREDITS = 444, QUIT = 486, INCREMENT = 42};

		TM->Draw(altMainMenuID,0,0);
		// Draw Menu items
		GroupFont->PrintTCHAR(playStr,		Game::GetInstance()->GetScreenWidth()/2-100,	PLAY, CursorY==PLAY ? 1.5f : 1.0f,			CursorY==PLAY ? isSelected : notSelected);
		GroupFont->PrintTCHAR(optionsStr,	Game::GetInstance()->GetScreenWidth()/2-100,	OPTIONS, CursorY==OPTIONS ? 1.5f : 1.0f,	CursorY==OPTIONS ? isSelected : notSelected);
		GroupFont->PrintTCHAR(htpStr,		Game::GetInstance()->GetScreenWidth()/2-100,	H2P, CursorY==H2P ? 1.5f : 1.0f,			CursorY==H2P ? isSelected : notSelected);
		GroupFont->PrintTCHAR(creditsStr,	Game::GetInstance()->GetScreenWidth()/2-100,	CREDITS, CursorY==CREDITS ? 1.5f : 1.0f,	CursorY==CREDITS ? isSelected : notSelected);
		GroupFont->PrintTCHAR(quitStr,		Game::GetInstance()->GetScreenWidth()/2-100,	QUIT, CursorY==QUIT ? 1.5f : 1.0f,			CursorY==QUIT ? isSelected : notSelected);
		
		RECT cursor = {};//{18,18,66,74};
		cursor.left = (LONG)18;
		cursor.top = (LONG)18;
		cursor.right = (LONG)66;
		cursor.bottom = (LONG)74;
		TM->Draw(menuStuffID,CursorX,CursorY,1.0f,1.0f,&cursor);
	}

}

MainMenuState* MainMenuState::GetInstance(void)
{
	// Lazy instantiation
	static MainMenuState MainInstance;

	return &MainInstance;
}